#include "stdafx.h"
#include "stdlib/stopwatch.hpp"


namespace JHCPP
{
	namespace stdlib
	{
		CStopWatch::CStopWatch() 
		: m_elapsed(0),
		m_running(false)
		{
		}

		CStopWatch::~CStopWatch()
		{
		}

		void CStopWatch::start()
		{
			if (!m_running)
			{
				m_start.update();
				m_running = true;
			}
		}

		void CStopWatch::stop()
		{
			if (m_running)
			{
				CClock current;
				m_elapsed += current - m_start;
				m_running = false;
			}
		}

		void CStopWatch::reset()
		{
			m_elapsed = 0;
			m_running = false;
		}

		void CStopWatch::restart()
		{
			m_elapsed = 0;
			m_start.update();
			m_running = true;
		}

		CClock::ClockDiff CStopWatch::elapsed() const
		{
			if (m_running)
			{
				CClock current;
				return m_elapsed + (current - m_start);
			}
			else
			{
				return m_elapsed;
			}
		}

		int CStopWatch::elapsedSeconds() const
		{
			return int(elapsed()/resolution());
		}

		CClock::ClockVal CStopWatch::resolution()
		{
			return CClock::resolution();
		}

	}//end of namespace stdlib
}//end of namespace JHCPP
